<template>
    <el-form ref="registerFormRef" :model="registerForm" @keyup.enter="onRegister">
        <div class="register-title">用户注册</div>
        <el-form-item>
            <el-input v-model="registerForm.userName" :prefix-icon="MostlyCloudy" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item required>
            <el-input v-model="registerForm.userAccount" :prefix-icon="User" placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
            <el-input v-model="registerForm.userPassword" :prefix-icon="Lock" show-password placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="用户头像：">
            <el-upload class="avatar-uploader" :action="''" :auto-upload="false" :show-file-list="false"
                :on-change="handleAvatarChangeIcon">
                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                <el-icon v-else class="avatar-uploader-icon">
                    <Plus />
                </el-icon>
            </el-upload>
        </el-form-item>
        <el-form-item class="register-button">
            <el-button type="primary" @click="onRegister()">注册</el-button>
        </el-form-item>
    </el-form>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { User, Lock, MostlyCloudy, Plus } from '@element-plus/icons-vue'
import { useRouter,useRoute } from 'vue-router';
import { useUploadPictureApi } from '@/api/user/user'
import { ElMessage, type UploadProps } from 'element-plus'
import { type UserRegister } from '@/model/model3000'
import { useRegisterApi } from '@/api/user/user';
import service from '@/api/request.js';

const router = useRouter()
const route = useRoute()
const registerForm = reactive({
    userName: '',
    userAccount: '',
    userPassword: '',
    userPhoto: '',
})

const imageUrl = ref()

const handleAvatarChangeIcon: UploadProps['onChange'] = (file, fileList) => {
    const isJPG = file.raw!.type === 'image/jpeg'
    const isPNG = file.raw!.type === 'image/png'
    // const isLt2M = file.raw.size / 1024 / 1024 < 0.5 //限制上传文件的大小
    if (!isPNG && !isJPG) {
        ElMessage({
            message: '上传图片只能是 JPG/PNG 格式!',
            type: 'warning'
        })
        return false
    } else {
        imageUrl.value = URL.createObjectURL(file.raw!);//赋值图片的url，用于图片回显功能
        uploadImg(file)//调用上传图片api接口
    }
}
const uploadImg = async (file: any) => {
    let formData = new FormData()
    formData.append('img', file.raw)
    await useUploadPictureApi(formData)
        .then((res) => {
            const { data } = res
            registerForm.userPhoto = data
        })
}

const onRegister = async () => {
    // 用户注册
    await useRegisterApi(registerForm)
    .then((res)=>{
        ElMessage({
            message:'注册成功',
            type:'success'
        })
        refresh()
    })
    
}
function refresh() {
    // 刷新当前路由
    const { fullPath } = route
    router.replace({
        path: '/redirect' + fullPath
    })
}

</script>

<style lang="scss" scoped>
.register-title {
    display: flex;
    justify-content: center;
    margin-bottom: 35px;
    font-size: 24px;
    color: #444;
    letter-spacing: 4px;
}

.register-button {
    :deep(.el-button--primary) {
        margin-top: 10px;
        width: 100%;
        height: 45px;
        font-size: 18px;
        letter-spacing: 8px;
    }
}

.avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
}

.avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
}
</style>